﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2024/7/26 11:30:36------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class products_goods_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int category;
        protected int status;
        protected int products;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.category = DTRequest.GetQueryInt("category");
            this.products = DTRequest.GetQueryInt("products");
            this.status = DTRequest.GetQueryInt("status");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("products_goods", DTEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind();
                RptBind("id>0" + CombSqlTxt(keywords), "sort_id asc,id desc");
            }
        }

        #region 绑定类别=================================
        private void TreeBind()
        {
            BLL.products_category bll = new BLL.products_category();
            DataTable dt = bll.GetList(0);

            this.ddlCategory.Items.Clear();
            this.ddlCategory.Items.Add(new ListItem("所有类别", ""));

            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlCategory.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlCategory.Items.Add(new ListItem(Title, Id));
                }
            }                       

            ProductsBind();
        }
        #endregion

        #region 绑定产品=================================
        private void ProductsBind()
        {
            this.ddlProducts.Items.Clear();
            this.ddlProducts.Items.Add(new ListItem("所有产品", ""));

            if (category>0)
            {
                BLL.products bll = new BLL.products();
                DataSet dt = bll.GetList(0, "category_id in(select id from dt_products_category where class_list like '%," + category + ",%')", "sort_id");

                if (Utils.CheckDataSet(dt))
                {
                    foreach (DataRow dr in dt.Tables[0].Rows)
                    {
                        string Id = dr["id"].ToString();
                        string Title = dr["title"].ToString().Trim();
                        this.ddlProducts.Items.Add(new ListItem(Title, Id));
                    }
                }
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);

            this.ddlCategory.SelectedValue = category.ToString();
            this.ddlStatus.SelectedValue = this.status.ToString();
            this.ddlProducts.SelectedValue = this.products.ToString();
            this.txtKeywords.Text = this.keywords;

            //图表或列表显示
            BLL.products_goods bll = new BLL.products_goods();

            this.rptList.DataSource = bll.GetListView(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("products_goods_list.aspx", "status={0}&category={1}&keywords={2}&products={3}&page={4}",
               this.status.ToString(), this.category.ToString(), this.keywords, this.products.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords
                    + "%' or sub_title like '%" + _keywords
                    + "%' or category_title like '%" + _keywords
                    + "%' or product_title like '%" + _keywords
                    + "%' or sell_price like '%" + _keywords
                    + "%' or abstracts like '%" + _keywords
                    + "%' or contents like '%" + _keywords + "%')");
            }

            switch (status)
            {
                case 1:
                    strTemp.Append(" and status=0");
                    break;
                case 2:
                    strTemp.Append(" and status=1");
                    break;
            }

            if (products > 0)
            {
                strTemp.Append(" and products_id=" + products);
            }

            if (category > 0)
            {
                strTemp.Append(" and category_id in(select id from dt_products_category where class_list like '%," + category + ",%')");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("products_goods_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        protected void Redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("products_goods_list.aspx", "status={0}&category={1}&keywords={2}&products={3}",
                ddlStatus.SelectedValue, ddlCategory.SelectedValue, txtKeywords.Text.Trim(), ddlProducts.SelectedValue));
        }

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("products_goods_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("products_goods_list.aspx", "keywords={0}", this.keywords));
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("products_goods", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.products_goods bll = new BLL.products_goods();
            
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "保存产品规格排序"); //记录日志
            JscriptMsg("保存排序成功！", Utils.CombUrlTxt("products_goods_list.aspx", Utils.CombUrlTxt("products_goods_list.aspx", "status={0}&category={1}&keywords={2}&products={3}", this.status.ToString(), this.category.ToString(), this.keywords, this.products.ToString())));
        }


        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("products_goods", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.products_goods bll = new BLL.products_goods();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "删除产品规格成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("products_goods_list.aspx", "status={0}&category={1}&keywords={2}&products={3}", this.status.ToString(), this.category.ToString(), this.keywords, this.products.ToString()));
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProducts.SelectedIndex = 0;
            Redirect();
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }
    }
}

